<template>
  <div>

  <div style="float:left;">
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定批量删除这些数据吗？"
              @confirm="delBatch()"
          >
            <el-button type="danger" size="medium" slot="reference">批量删除 <i class="el-icon-delete-solid"></i></el-button>
          </el-popconfirm>
  </div>
   <el-table
      :data="tableData"
      v-loading="loading"
      stripe
      style="width: 100%"
      tooltip-effect="dark"
      @selection-change="handleSelectionChange"
      >
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="id"
        label="评论编号"
        >
      </el-table-column>
      <el-table-column
      prop="picture"
      label="博客图片"
      >
<template slot-scope="scope">
<viewer>
<img :src="scope.row.picture" width="60px" />
</viewer>
</template>
  </el-table-column>

      <el-table-column
        prop="blogAuthor"
        label="评论人"
        >
      </el-table-column>
      <el-table-column
        prop="title"
        label="评论内容"
        >
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="评论时间"
        :formatter="formatTime"
        width="180">
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
       >
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row.id)" type="text" size="small">查看评论</el-button>
        <el-popconfirm
    confirm-button-text='确定'
    cancel-button-text='取消'
    icon="el-icon-info"
    icon-color="red"
    title="确定删除这段内容吗？"
    @confirm="deleteById(scope.row.id)"
  >
    <el-button slot="reference" type="text"  style="margin-left:10px">删除</el-button>
  </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-empty :image-size="200" v-if="flag==false"></el-empty>
    <el-dialog  title="查看评论信息" style="text-align:left" :visible.sync="dialogFormVisible">
    <div >
      <ul v-for="(item,index) in items" :key="index">
        <li >评论人:{{item.userName}} </li>
        <li>评论内容:{{item.reply}}</li>
      </ul>
    </div>
     <div slot="footer" class="dialog-footer">
    <el-button @click="back()">取 消</el-button>
    <el-button type="primary" @click="dialogFormVisible=false">确 定</el-button>
  </div>
  </el-dialog>
   <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNo"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
  </div>
  </template>

<script>
import moment from 'moment'
export default {
  name: 'CommentView',
  data () {
    return {
      drawer: false,
      flag: false,
      tableData: [],
      pageNo: 1,
      pageSize: 10,
      total: 0,
      comment: {
        author: '',
        comment: ''
      },
      multipleSelection: [],
      dialogFormVisible: false,
      loading: true,
      items: []
    }
  },
  methods: {
    // 格式化时间
    formatTime (row, column) {
      const date = row[column.property]
      if (date === undefined) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    delBatch () {
      const ids = this.multipleSelection.map(v => v.id)
      this.request.post('/blog/deleteByIds', ids).then(res => {
        if (res.code === '200') {
          this.$message({
            showClose: true,
            type: 'success',
            message: '操作成功'
          })
          this.loadGet()
        } else {
          this.$message({
            showClose: true,
            type: 'error',
            message: '网络异常,操作失败'
          })
        }
      })
    },
    deleteById (id) {
      this.request.post('/blog/delete/' + id).then(res => {
        if (res.code === '200') {
          this.loadGet()
          this.$message({
            showClose: true,
            message: '操作成功',
            type: 'success'
          })
        } else {
          this.$message({
            showClose: true,
            message: '操作失败',
            type: 'error'
          })
        }
      })
    },
    // 打开新增页面
    handleAdd () {
      this.drawer = true
    },
    back () {
      this.dialogFormVisible = false
    },
    async updateAdmin () {
      const user = {
        author: this.form.author,
        comment: this.form.comment,
        id: this.form.id
      }
      this.request.post('/comment/updateComment', user).then(res => {
        if (res.code === '200') {
          this.loadGet()
          this.dialogFormVisible = false
          this.$message.success('操作成功')
        } else {
          this.$message.danger('网络异常，修改失败')
        }
      })
    },
    handleDelete (id) {
      this.request.post('/comment/deleteById/' + id).then(res => {
        if (res.code === '200') {
          console.log(id)
          this.loadGet()
          this.dialogFormVisible = false
          this.$message.success('操作成功')
        } else {
          this.$message.danger('网络异常，修改失败')
        }
      })
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    reset () {
      this.adminuserName = ''
      this.loadGet()
    },
    search () {
      this.loadGet()
    },
    handleClick (id) {
      this.request.post('/blog/findAllByRemarkId' + '/' + id
      ).then(res => {
        this.items = res.data
      })
      this.dialogFormVisible = true
    },
    handleSizeChange (val) {
      this.pageSize = val
      this.loadGet()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.pageNo = val
      this.loadGet()
    },
    async loadGet () {
      this.request.post('/reply/commentlist' + '/' + this.pageNo + '/' + this.pageSize
      ).then(res => {
        this.flag = true
        this.tableData = res.data.list
        this.total = res.data.total
        this.loading = false
      })
    }
  },
  mounted () {
    this.loadGet()
  }
}
</script>

  <style>
  .div{
    margin-top:40px;
    margin-bottom: 20px;
  }
  ul{
    list-style: none;
  }
  </style>
